#include <xmlparser.h>
#include <iostream>


XMLParser::XMLParser(Building* b)
{
    building=b;
   // makeCurrent();

}

void XMLParser::parse()
{
    QString filename("building.xml");
    int errorLine, errorColumn;
    QString errorMsg;
    QFile modelFile(filename);
    QDomDocument document;
    if(!document.setContent(&modelFile,&errorMsg,&errorLine,&errorColumn))
    {
        QString error("gebt nen error dommerik!!!!");
        std::cout<< error.toStdString()+"\n";
        //return false;
    }

    QDomElement rootElement=document.documentElement();


    for(QDomNode node =rootElement.firstChild();!node.isNull();node=node.nextSibling())
    {


        if(node.toElement().tagName()=="variables")
        {

            setVariables(node);
        }
        if(node.toElement().tagName()=="properties")
        {

            setProperties(node);
        }
        if(node.toElement().tagName()=="objects")
        {

            object=makeObjects(node);
            building->setObject(object);
            std::cout <<"done\n";

        }
    }
}
GLuint XMLParser::makeObjects(QDomNode n)
{
    QDomElement element=n.firstChild().toElement();
    QVector<GLuint> v;
    while(!element.isNull())
    {
        if(element.tagName()=="object")
        {
            v.append(makeObject(element));
        }
        element=element.nextSibling().toElement();
    }

    int i=0;
    GLuint list=0;
    list=glGenLists(1);

    glNewList(list,GL_COMPILE);
    glDisable( GL_LIGHTING );;
    do
    {
        if(glIsList(v.value(i)))
        {
            std::cout << "ok \n";
        }
        glCallList(v.value(i));

    } while(v.value(i)!=v.last());

    glEnable( GL_LIGHTING );
    glEndList();

    return list;
}

GLuint XMLParser::makeObject(QDomElement e)
{
    if(e.attribute("type")=="polygon")
    {
        return drawPolygon(e);

    }
    return 1;

}
GLuint XMLParser:: drawPolygon(QDomElement e)
{
    GLuint list=1;
    list=glGenLists(1);
    if(glIsList(list)!=GL_TRUE)
    {
        std::cout<<"geen lijst\n";
    }
    glNewList(list,GL_COMPILE);

    //const QColor c(255,255,255);
    //qglColor(c);

    // glTranslatef(1.5f,0.0f,-15.0f);
glPushMatrix();
   // glDisable( GL_LIGHTING );

glLoadIdentity();
    glBegin(GL_QUADS);

    QDomElement element=e.firstChild().toElement();
    while(!element.isNull())
    { glColor3f(1.0, 0.0, 1.0);
        glVertex3fv(getPoint(element));
        element=element.nextSibling().toElement();
    }
    glEnd();

    //glEnable( GL_LIGHTING );

glPopMatrix();
    glEndList();
    int error = glGetError();
    if (error != GL_NO_ERROR) {
      std::cout << "An OpenGL error has occured: " << gluErrorString(error) << std::endl;
    }
    else
    {
        std::cout<<"test\n";
    }


    if(glIsList(list)==GL_TRUE)
    {
        std::cout<<" lijst\n";
    }
    return list;
}

GLfloat* XMLParser::getPoint(QDomElement e)
{

    if(e.attribute("ref")!="none")
    {
        GLfloat* f=new GLfloat;
        QVector<GLfloat> v=points.find(e.attribute("ref")).value();
        for(int i=0;i<3;i++)
        {
            f[i]=v.value(i);

        }


        return f;
    }
    else
    {
        return NULL;
    }
}

void XMLParser::setVariables(QDomNode n)
{

    QDomNode node=n.firstChild();
    while(!node.isNull()){
        if(node.toElement().tagName()=="points")
        {
            for(QDomElement child =node.firstChild().toElement();!child.isNull();child=child.nextSibling().toElement())
            {
                if(child.toElement().tagName()=="point")
                {
                    QVector<GLfloat> vector;
                    vector.append(child.attribute("x").toFloat());
                    vector.append(child.attribute("y").toFloat());
                    vector.append(child.attribute("z").toFloat());

                    points[child.attribute("id")]=vector;

                }
            }
        }
        else if(node.toElement().tagName()=="list")
        {
            for(QDomElement child =node.firstChild().toElement();!child.isNull();child=child.nextSibling().toElement())
            {
                if(child.toElement().tagName()=="point")
                {
                    QVector<float> vector;
                    vector.append(child.attribute("x").toFloat());
                    vector.append(child.attribute("y").toFloat());
                    vector.append(child.attribute("z").toFloat());

                    points[child.attribute("id")]=vector;

                }
            }
        }
        node=node.nextSibling();
    }

}

void XMLParser::setProperties(QDomNode n)
{
    QDomNode node=n.firstChild();
    while(!node.isNull()){
        if(node.toElement().tagName()=="camera")
        {
            QVector<double> v;
            QDomElement n2=node.firstChild().toElement();
            while(!n2.isNull())
            {v.append(n2.attribute("x").toDouble());
                v.append(n2.attribute("y").toDouble());
                v.append(n2.attribute("z").toDouble());
                n2=n2.nextSibling().toElement();
            }
            building->setCamera(v);
        }
        else if(node.toElement().tagName()=="lights")
        {

        }
        node=node.nextSibling();
    }
}
